﻿namespace BlackHole.DAO
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    public static partial class Utility
    {
        public static bool CheckProdcutImport(ref ImportRecord record)
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("VaildateProdcutImport", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@serialNO", record.ProductSerialNO);
                cmd.Parameters.AddWithValue("@deviceId", record.ProductDeviceId);
                cmd.Parameters.AddWithValue("@ownerId", record.OwnerId);
                cmd.Parameters.AddWithValue("@psamNO", record.ProductPsamNO);
                cmd.Parameters.AddWithValue("@romVersion", record.ProductRomVersion);
                cmd.Parameters.AddWithValue("@isRecord", (record.IsRecord ? 1 : 0));
                cmd.Parameters.Add("@RETURN", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                try
                {
                    // 返回值为是否成功
                    // scalar值为productid
                    object pid = cmd.ExecuteScalar();
                    if (pid != null) 
                    {
                        // 是record
                        record.ProductId = (int)pid;
                    } 

                    int ret = (int)cmd.Parameters["@RETURN"].Value;
                    return
                        ret != 0 ? true : false;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}